<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <PageTools>
          <template v-slot:after>
            <el-button size="small" type="primary" @click="addRole">添加角色</el-button>
          </template>
        </Pagetools>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column label="序列" width="120" type="index" align="center" />
          <el-table-column label="角色名称" width="240" prop="title" align="center" />
          <el-table-column label="角色描述" prop="description" header-align="center" />
          <el-table-column label="操作" width="240" header-align="center">
            <template v-slot="{row}">
              <el-button size="small" type="success" @click="assignPerm(row)">分配权限</el-button>
              <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <AddRole ref="editrole" :show-dialog.sync="showDialog" @getRolesList="getRolesList" />
        <!-- 分页 -->
        <el-row type="flex" justify="center">
          <el-col :span="8">
            <el-pagination
              background
              :page-size="page._limit"
              :current-page="pages"
              layout="prev, pager, next"
              :total="total"
              @current-change="changeCur"
            />
          </el-col>
        </el-row>

        <!-- 分配权限 -->
        <el-dialog title="分配权限" :visible="isShowPermission" @close="btnPermCancel">
          <el-tree
            ref="tree"
            :data="permissionList"
            :props="{label: 'title'}"
            default-expand-all
            :check-strictly="true"
            node-key="id"
            show-checkbox
          />
          <template #footer>
            <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </template>
        </el-dialog>

      </el-card>
    </div>
  </div>
</template>

<script>
import { getRolesList, deleteRoles, okRoles, getRoles } from '@/api/roles'
import { getPurviewList } from '@/api/permission'
import { listToTree } from '@/utils/index'
import AddRole from './components/add-role.vue'
export default {
  components: { AddRole },
  data() {
    return {
      tableData: [],
      showDialog: false, // 控制弹窗
      page: {
        _limit: 5,
        _start: 0
      },
      total: 0, // 总数
      pages: 1, // 页码
      isShowPermission: false, // 分配权限
      permissionList: [],
      id: ''
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    changeCur(newPage) {
      this.pages = newPage
      this.page._start = (parseInt(this.pages) - 1) * parseInt(this.page._limit)
      this.getRolesList(this.page)
    },
    // 获取角色
    async getRolesList() {
      this.tableData = await getRolesList(this.page)
      const data = await getRolesList()
      this.total = data.length
    },
    // 添加角色
    addRole() {
      this.showDialog = true
    },
    // 分配权限
    async assignPerm(row) {
      const rowId = await getRoles(row.id)
      this.id = rowId.id

      // 获取页面所有的的权限
      this.permissionList = listToTree(await getPurviewList(), '0')
      // 当前是该用户可以拥有的权限点
      // console.log(this.permissionList)
      this.$nextTick(() => {
        const arr = rowId.permissions.map(item => item.id)
        // console.log(arr)
        this.$refs.tree.setCheckedKeys(arr)
      })
      this.isShowPermission = true
    },
    // 分配权限弹窗关闭
    btnPermCancel() {
      this.isShowPermission = false
    },
    // 确定修改权限
    async btnPermOK() {
      const permIds = this.$refs.tree.getCheckedKeys()
      // console.log(this.id)
      await okRoles(this.id, permIds)
      this.btnPermCancel()
      this.$message({
        message: '修改成功',
        type: 'success'
      })
    },
    // 编辑角色
    async editRole(id) {
      this.$refs.editrole.id = id
      await this.$refs.editrole.editRole()
      this.showDialog = true
    },
    // 删除角色
    async deleteRole(id) {
      await this.$confirm('确定要删除权限? 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await deleteRoles(id)
      if (this.tableData.length === 1 && this.pages !== 1) {
        this.page._start = (parseInt(this.pages - 1) - 1) * parseInt(this.page._limit)
        // console.log(this.page._start)
      }
      this.getRolesList()
      this.$message({
        message: '删除成功！',
        type: 'success'
      })
    }

  }
}
</script>

<style>

</style>
